<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 输入内容区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="goodsList" border :stripe="true" width="100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="160px"></el-table-column>
        <el-table-column label="商品数量" prop="goods_number" width="160px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editGoodsById(scope.row.goods_id)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20, 25]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>

    <!-- 编辑商品对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editListDialogVisible" width="34%">
      <el-form :model="editListForm" :rules="editListFormRules" ref="editListFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editListForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editListForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="editListForm.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editListDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEditRoot">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'List',
  data() {
    return {
      // 商品列表
      goodsList: [],
      // 查询列表
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品总数
      total: 0,
      // 编辑对话框显示与隐藏
      editListDialogVisible: false,
      // 编辑商品表单数据
      editListForm: {
        goods_name: '',
        goods_price: '',
        goods_number: ''
      },
      editListFormRules: {
        goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' }]
      },
      // 商品id
      goods_id: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  // 获取商品列表参数
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表参数失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 编辑按钮 弹出对话框
    async editGoodsById(ids) {
      this.goods_id = ids
      // 根据id查询 当前的商品数据
      const { data: res } = await this.$http.get(`goods/${ids}`)
      if (res.meta.status !== 200) {
        return this.$message.error('查询id商品数据失败')
      }
      this.editListForm = res.data
      this.editListDialogVisible = true
    },
    // 编辑商品
    addEditRoot() {
      this.$refs.editListFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必选的表单项!')
        } else {
          const { data: res } = await this.$http.put(`goods/${this.goods_id}`, this.editListForm)
          if (res.meta.status !== 200) {
            return this.$message.error('修改商品参数失败')
          }
          this.getGoodsList()
          this.editListDialogVisible = false
          this.$message.success('修改商品参数成功')
        }
      })
    },

    // 删除商品
    async removeById(ids) {
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        return err
      })
      // 如果用户确认删除, 则返回值为字符串 confirm
      // 如果用户取消删除, 则返回值为字符串 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除!')
      } else {
        const { data: res } = await this.$http.delete(`goods/${ids}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除商品失败')
        }
        this.getGoodsList()
        this.$message.success('删除商品成功')
      }
    },
    // 跳转页面添加
    goAddpage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style></style>
